<?php

declare(strict_types=1);

namespace app\index\controller;

use app\common\model\Assets;
use app\common\model\Bank;
use app\common\model\Relation;
use app\common\model\SignInfo;
use app\common\model\UserSign;
use app\common\model\UserRelease;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;
use app\common\model\User as UserModel;
use app\common\model\TaskList;
use app\common\controller\Config;

class User extends Base
{

    public function index()
    {
        $sysm = Config::lists();
        $taskcount = TaskList::where('uid', $this->user->id)->whereDay('create_time')->count();
        $assets = Assets::getFields(3);
        unset($assets['green_integral']);
        unset($assets['frost_integral']);
        // print('<pre>');print_r($assets);die;
        View::assign(
            [
                'user' => $this->user,
                'sysm' => $sysm,
                'taskcount' => $taskcount,
                'assets' => $assets
            ]
        );
        return View::fetch();
    }
    /**
     *交易大厅
     */
    public function deal()
    {   
        $sysm = Config::lists();
        $user = $this->user;
        $open = 0;
        if((!$user['bank_open'] || !$user['bank_number'] || !$user['bank_name'] || !$user['bank_account_name']) && (!$user['wechat_name'] || !$user['wechat_image']) && (!$user['alipay_name'] || !$user['alipay_image'])){
            $open = 1;
        }
        View::assign(
            [
                'open'=>$open,
                'money'=>$sysm['buy_money'],
            ]
        );
        return View::fetch();
    }

    public function info()
    {
        return View::fetch();
    }

    public function setting()
    {
        return View::fetch();
    }

    public function contact()
    {
        return View::fetch();
    }

    public function my_team()
    {
        if ($this->request->isAjax()) {
            $page = $this->request->param('page', 1);
            $map = ['uid' => $this->user->id, 'algebra' => 1];
            return json(Relation::getPageList($map, '*', $page, 20, false, ['fresh']));
        }
        $dpn = Relation::where(['uid' => $this->user->id, 'algebra' => 1])->column('new_uid');
        $team = Relation::where('uid', $this->user->id)->count();
        $min_team_total = UserModel::geMinTeam($this->user->id);
        View::assign([
            'dpn'        => count($dpn),
            'dpn_total'  => UserModel::whereIn('id', $dpn)->sum('my_total'),
            'team'       => $team,
            'team_total' => $this->user->team_total,
            'min_team_total' => $min_team_total
        ]);
        return View::fetch();
    }
    /**
     * 抽奖
     */
    public function lotto()
    {
        $SignInfo = SignInfo::order('id asc')->select();

        $taskcount = UserSign::where('uid', $this->user->id)->whereDay('create_time')->count();
        foreach ($SignInfo as $k => $v) {
            if ($k % 2 == 0) {
                $color = '#EC1B24';
            } else {
                $color = '#FFFFFF';
            }
            $colors[] = $color;
            $titles[] = $v['title'];
            $probs[] = ($v['prob'] * 100) . "%";
        }
        //print('<pre>');print_r($titles);die;
        View::assign([
            'restaraunts' => json_encode($titles),
            'colors' => json_encode($colors),
            'randomRate' => json_encode($probs),
            'taskcount' => $taskcount,
            'level_id' => $this->user->level_id,
            'is_sign' => $this->user->is_sign,
            'is_out' => $this->user->is_out,
        ]);
        return View::fetch();
    }
    /**
     * 抽奖提交奖项
     */
    public function lottoPost()
    {
        try {
            $param = $this->request->post();
            if ($this->user->level_id == 0) {
                return Result::fail('请激活后在进行签到！');
            }
            $taskcount = UserSign::where('uid', $this->user->id)->whereDay('create_time')->count();
            if (!empty($taskcount)) {
                return Result::fail('今天已签到！');
            }
            $SignInfo = SignInfo::order('id asc')->select();
            return Result::fastReturn(UserSign::userSignAdd($this->user, $SignInfo[$param['item'] - 1]['id']));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    public function methodList()
    {
        return View::fetch();
    }

    public function method($type)
    {
        $mode = ['alipay' => '支付宝', 'wechat' => '微信', 'bank' => '银行卡'];
        View::assign([
            'type'      => $type,
            'title'     => $mode[$type],
            'bank_list' => Bank::where(['status' => 1])->column('title', 'id')
        ]);
        return View::fetch();
    }

    public function method_save($type)
    {
        try {
            $param = $this->request->post(['name', 'image', 'bank_name', 'bank_open', 'bank_account_name', 'bank_number']);

            $v = $type == 'bank' ? 'MethodBank' : 'MethodImg';
            try {
                $this->validate($param, $v);
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            switch ($type) {
                case "bank":
                    break;
                default:
                    $param[$type . '_name'] = $param['name'];
                    $param[$type . '_image'] = $param['image'];
                    unset($param['name']);
                    unset($param['image']);
            }

            UserModel::update($param, ['id' => $this->user->id]);

            return json(['code' => 1, 'msg' => '已保存']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function edit()
    {
        try {
            $param = $this->request->post(['nickname', 'photo','mobile']);
            UserModel::update($param, ['id' => $this->user->id]);
            return Result::success('已修改');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    public function logout()
    {
        $token = md5(time() . $this->user['id'] . $this->request->ip());

        UserModel::update([
            'token' => $token
        ], ['id' => $this->user['id']]);
        cookie('token', null);

        return Result::fastReturn(['code' => 1, 'msg' => '已退出登录', 'url' => url('login/index')->build()]);
    }

    public function share()
    {
        $url = "http://" . $_SERVER['SERVER_NAME'] . "/index/register/index.html?code=" . $this->user->code;
        View::assign('url', $url);
        return View::fetch();
    }
    /**
     * 公司宣传片
     */
    public function video()
    {
        $sysm = Config::lists();
        $taskcount = TaskList::where('uid', $this->user->id)->whereDay('create_time')->count();

        View::assign(
            [
                'user' => $this->user,
                'sysm' => $sysm,
                'taskcount' => $taskcount,
                'assets' => Assets::getFields(3)
            ]
        );
        return View::fetch();
        return View::fetch();
    }

    public function release()
    {
        if ($this->request->isAjax()) {
            return Result::fastReturn(UserRelease::getPageList(['uid' => $this->user->id], '*', $this->getPage(), 20, false));
        }
        return View::fetch();
    }
}
